package com.sanrenzu.everybodytv.ui.column.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.zxb.VideoItemChildBean;
import com.sanrenzu.everybodytv.widget.GlideRoundTransform;

import java.util.List;

/**
 * Created by My on 2016/9/6.
 */
public class VideoItemChildAdapter extends BaseAdapter {
    private List<VideoItemChildBean> list = null;
    private Context context;
    private LayoutInflater inflater;
    private  mViewHolder mHolder = null;;

    public VideoItemChildAdapter(Context context, List<VideoItemChildBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = inflater.inflate(R.layout.adapter_videochild,parent,false);
            mHolder = new mViewHolder(convertView);
            convertView.setTag(mHolder);
        }else {
            mHolder = (mViewHolder) convertView.getTag();
        }

        mHolder.videochild_nick.setText(list.get(position).getNick());
        mHolder.videochild_title.setText(list.get(position).getTitle());
        mHolder.videochild_text_view.setText(list.get(position).getView());

        Glide.with(context).load(list.get(position).getThumb()+"")
                .placeholder(R.drawable.defaultmap)
                .error(R.drawable.defaultsucceful)
                .into(mHolder.videochild_thumb);

        //将显示的图片设置成圆形
        Glide.with(context).load(list.get(position).getAvatar()+"").asBitmap().centerCrop().into
                (new BitmapImageViewTarget(mHolder.videochild_avatar) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(),
                                        resource);
                        mHolder.videochild_avatar.setImageDrawable(circularBitmapDrawable);
                    }
                });
        Glide.with(context).load(list.get(position).getAvatar()+"").transform(new GlideRoundTransform(context))
                .into(mHolder.videochild_avatar);

        return convertView;
    }

    class mViewHolder {
        ImageView videochild_thumb;
        ImageView videochild_avatar;
        TextView videochild_nick;
        TextView videochild_title;
        TextView videochild_text_view;

        public mViewHolder(View convertView) {
            videochild_thumb = (ImageView) convertView.findViewById(R.id.videochild_thumb);
            videochild_avatar = (ImageView) convertView.findViewById(R.id.videochild_avatar);
            videochild_nick = (TextView) convertView.findViewById(R.id.videochild_nick);
            videochild_title = (TextView) convertView.findViewById(R.id.videochild_title);
            videochild_text_view = (TextView) convertView.findViewById(R.id.videochild_text_view);

        }
    }
}
